package com.jright.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jright.po.Student;
import com.jright.po.Thesis;
import com.jright.po.Topic;
import com.jright.service.StudentService;
import com.jright.service.ThesisService;
import com.jright.service.TopicService;
import com.jright.tools.Times;

import net.sf.json.JSONObject;

@Controller
public class StudentController {

	@Autowired
	private TopicService topicService;
	@Autowired
	private StudentService studentService;
	@Autowired
	private ThesisService thesisService;
	
	@PostMapping("/student/publishTopic")
	@ResponseBody
	public JSONObject publishTopic(@RequestBody String json) {
		JSONObject data = JSONObject.fromObject(json); 
		String title = data.getString("title"); 
		String publish_no = data.getString("publish_no"); 
		String introduction = data.getString("introduction"); 
		int row = this.topicService.publishMyTopic(title, publish_no, introduction);
		JSONObject json1 = new JSONObject();
		if(row > 0) {
			json1.put("msg","上传成功！");
		}else
			json1.put("msg","上传失败！");
		return json1;
	}
	
	@RequestMapping("/student/toChoosetopic")
	public String toChoosetopic(HttpSession session, HttpServletRequest request) {
		Student student = (Student)session.getAttribute("student");
		String no = student.getNo();
		String teacher_no = this.studentService.findTeacherNo(no);
		List<Topic> teacherTopicList = this.topicService.teacherTopic(teacher_no);
		List<Topic> myPublishTopic = this.topicService.myPublishTopic(no);
		request.setAttribute("teacherTopicList", teacherTopicList);
		request.setAttribute("myPublishTopic", myPublishTopic);
		return "student/choosetopic";
	}
	
	@RequestMapping("/student/publishThesis")
	@ResponseBody
	public JSONObject publishThesis(@RequestBody Thesis thesis,HttpSession session) {
		Student student = (Student) session.getAttribute("student");
//		System.out.println(student.getNo());
		thesis.setTime(new Times().getTimes());
//		System.out.println(thesis.toString());
		int row = this.thesisService.publishThesis(thesis, student.getNo());
		this.studentService.updateStudentStatus(thesis.getStage(), student.getNo());
		JSONObject json1 = new JSONObject();
		if(row > 0) {
			json1.put("msg","提交论文成功！");
		}else
			json1.put("msg","提交论文失败！");
		return json1;
	}
	
	@RequestMapping("/student/toPublishtopic")
	public String toPublishtopic() {
		return "student/publishtopic";
	}
	@RequestMapping("/student/selectTopic")
	@ResponseBody
	public JSONObject selectTopic(@RequestBody String json) {
		System.out.println(json);
		JSONObject data = JSONObject.fromObject(json);
		int topic_id = Integer.parseInt(data.getString("topic_id"));
		String no = data.getString("student_no");
		Student student = this.studentService.wetherSelected(no);
		JSONObject json1 = new JSONObject();
		if(student == null) {
			int row = this.studentService.selectTopic(topic_id, no);
			if(row > 0) {
				int row1 = this.studentService.updateStudentStatus("未审核",no);
				int row2 = this.topicService.updateTopicStatus("未审核",topic_id);
				json1.put("msg","选题成功！");
			}else
				json1.put("msg","选题失败！");
		}else
			json1.put("msg","你已经选了题目，不能再次选择！");
		return json1;
	}
	
	@RequestMapping("/student/toIndex")
	public String toIndex(HttpSession session) {
		Student student = (Student) session.getAttribute("student");
		student = this.studentService.findStudentByNo(student.getNo());
		session.setAttribute("student", student);
		return "student/index";
	}
	
	@RequestMapping("/student/toMythesis")
	public String toMythesis(HttpServletRequest request, HttpSession session) {
		Student student = (Student) session.getAttribute("student");
		List<Thesis> thesisList = this.thesisService.findThesisBySno(student.getNo());
		request.setAttribute("thesisList", thesisList);
		return "student/mythesis";
	}
	
	@RequestMapping("/student/toGroupsituation")
	public String toGroupsituation(HttpServletRequest request, HttpSession session) {
		Student student = (Student) session.getAttribute("student");
		String teacher_no = this.studentService.findTeacherNo(student.getNo());
		List<Student> studentList = this.studentService.findStudentByTeacherNo(teacher_no);
		for(Student students : studentList) {
			System.out.println(students.toString());
		}
		request.setAttribute("studentList", studentList);
		return "student/groupsituation";
	}
	
	@RequestMapping("/student/toPostthesis")
	public String toPostthesis() {
		return "student/postthesis";
	}
	
}
